package com.whq.common;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 
 * Collection 工具类
 * 
 * @author wanghuaiqiang
 *
 */
public class CollectionUtil {
	/**
	 * 将 Map 的键值互转
	 * 
	 * @param <K>
	 * @param <V>
	 * @param map
	 * @return
	 *
	 * @author Gao Baowen
	 * @since 2009-4-16 上午11:06:18
	 */
	public static <K, V> Map<V, K> convertKey2Value(Map<K, V> map) {
		if (map == null) {
			return null;
		}
		Map<V, K> result = new LinkedHashMap<V, K>();
		for (Iterator<K> i = map.keySet().iterator(); i.hasNext();) {
			K key = i.next();
			result.put(map.get(key), key);
		}
		return result;
	}

	/**
	 * collection转换成字符串
	 * 
	 * @param c
	 * @param separator
	 * @return
	 */
	public static String collection2String(Collection c, String separator) {
		if (c == null) {
			return "";
		}

		StringBuilder sb = new StringBuilder();
		int k = 0;
		for (Iterator<Object> i = c.iterator(); i.hasNext();) {
			if (k > 0) {
				sb.append(separator);
			}
			sb.append(i.next().toString());
			k++;
		}
		return sb.toString();
	}

	/**
	 * 将 Set 转换成字符串(排除指定的字符串: except)
	 * 
	 * @param set
	 * @param split
	 * @param except
	 * @return
	 */
	public static String convertSet2StringExceptOne(Set set, String split, String except) {
		StringBuffer sb = new StringBuffer("");
		if (set == null) {
			return sb.toString();
		} 
		int count = 0;
		for (Iterator<Object> iterator = set.iterator(); iterator.hasNext();) {
			Object object = (Object) iterator.next();
			if (object.toString().equals(except)) {
				continue;
			}
			if ((object.toString() == null) || (object.toString().trim() == "")) {
				continue;
			}
			if (count > 0) {
				sb.append(split);
			}
			sb.append(object.toString());
			count++;
		}
		return sb.toString();
	}

	/**
	 * 将 List 转换成字符串
	 * 
	 * @param list
	 * @param split
	 * @return
	 */
	public static String convertList2String(List list, String split) {
		StringBuffer sb = new StringBuffer("");
		if (list == null) {
			return sb.toString();
		}
		int count = 0;
		for (Iterator<Object> iterator = list.iterator(); iterator.hasNext();) {
			Object object = (Object) iterator.next();
			if ((object.toString() == null) || (object.toString().trim() == "")) {
				continue;
			}
			if (count > 0) {
				sb.append(split);
			}
			sb.append(object.toString());
			count++;
		}
		return sb.toString();
	}

	/**
	 * 将 Set 转换成字符串
	 * 
	 * @param set
	 * @param split
	 * @return
	 */
	public static String convertSet2String(Set set, String split) {
		StringBuffer sb = new StringBuffer("");
		if (set == null) {
			return sb.toString();
		}
		int count = 0;
		for (Iterator<Object> iterator = set.iterator(); iterator.hasNext();) {
			Object object = (Object) iterator.next();
			if ((object.toString() == null) || (object.toString().trim() == "")) {
				continue;
			}
			if (count > 0) {
				sb.append(split);
			}
			sb.append(object.toString());
			count++;
		}
		return sb.toString();
	}
}